import { has } from 'lodash/fp';
import { Message } from "element-ui";
import { isObject, isArray } from "cl-admin/utils";

import { validate } from "./validate";

//判断文件后缀名
export function getFileType(fileName) {
	let suffix = '';
	// 获取类型结果
	let result = '';
	try {
		const flieArr = fileName.split('.');
		suffix = flieArr[flieArr.length - 1];
	} catch (err) {
		suffix = '';
	}
	console.log(suffix)
	// 匹配 视频
	const videolist = ['mp4', 'm2v', 'mkv', 'rmvb', 'wmv', 'avi', 'flv', 'mov', 'm4v'];
	result = videolist.find(item => item === suffix);
	if (result) {
		return 'video';
	}
	// 图片格式
	const imglist = ['png', 'jpg', 'jpeg', 'bmp', 'gif'];
	// 进行图片匹配
	result = imglist.find(item => item === suffix);
	if (result) {
		return 'image';
	}

}

export function getDecType(type) {
	let name = ''
	switch (type) {
		case 1:
			name = '加微号-微信号'
			break;
		case 2:
			name = '加微号-手机号'
			break;
		case 3:
			name = '加微号-QQ号'
			break;
		case 4:
			name = '加微码-二维码'
			break;
		case 5:
			name = '企微码-企业微信码'
			break;
		case 99:
			name = '其他'
			break;
	}

	return name
}

/**
 * 对象是否为空
 */
export const isEmpty = (obj) => {
	let key;
	let empty = true;
	if (obj instanceof Object) {
		for (key in obj) {
			if (empty && has(obj, key)) {
				empty = false;
			}
		}
	}
	return empty;
};

/**
 * 格式化文本内容
 */
export const formatInput = (data) => {
	let resultStr = data.replace(/\ +/g, ""); //去掉空格
	resultStr = data.replace(/[ ]/g, ""); //去掉空格
	resultStr = data.replace(/[\r\n]/g, ""); //去掉回车换行
	return resultStr;
};

/**
 * 是否为真
 */
export const isTrue = (data) => {
	switch (typeof data) {
		//对象，数组，null
		case "object":
			return !isEmpty(data);
			break;
		case "number":
			return data == 0 ? false : true; //不等于0 都为真
			break;
		case "string":
			return data.trim().length > 0 ? true : false;
			break;
		case "boolean":
			return data;
			break;
		case "undefined":
			return false;
		default:
			return false;
	}
};
// import _ from "lodash";
//卡的类型
export const cardTypes = [
	{
		name: 1,
		title: "储值卡"
	},
	{
		name: 2,
		title: "打折卡"
	},
	{
		name: 3,
		title: "时段卡"
	},
	{
		name: 4,
		title: "疗程总次卡"
	},
	{
		name: 5,
		title: "疗程分次卡"
	}
]
//处理内容 type：卡类型 2打折卡 3时段卡 4疗程总次数 5疗程分次卡
export const HandleContent = (type, arr, ids, discount, allcount, cardContentType) => {
	console.log('cardContentType', cardContentType)
	console.log('type', type)
	let data;
	switch (type) {
		case 2:
			data = handleContent2(arr, type, ids, discount, cardContentType);
			break;
		case 3:
			data = handleContent2(arr, type, ids, discount, cardContentType);
			break;
		case 4:
			data = handleContent4(arr, type, ids, allcount, cardContentType);
			break;
		case 5:
			data = handleContent4(arr, type, ids, allcount, cardContentType);
			break
		case 6:
			console.log(type)
			data = handleContent6(arr, type);
			break
		default:
			data = usedHandleContent(arr);
			break;
	}
	return data;
}

//常用处理内容
export const usedHandleContent = (arr) => {
	console.log('常用处理内容', arr)
	let item = [],
		product = [],
		obj = {},
		content_data = {};
	for (const a of arr) {
		if (a.si_id) {
			obj = {
				id: a.si_id,
				count: a.count,
				specifications_id: a.specId
			};
			item.push(obj);
		} else {
			obj = {
				id: a.sr_id,
				count: a.count,
				specifications_id: a.specId
			};
			product.push(obj);
		}
	}
	if (product.length > 0) {
		content_data.product = product;
	}
	if (item.length > 0) {
		content_data.item = item;
	}
	console.log('content_data', content_data)
	return content_data;
}

//处理打折卡内容 ids:门店ids arr:提交的内容数组
export const handleContent2 = (arr, type, ids, discount, cardContentType) => {
	console.log('处理打折卡内容----------22222222', arr, ids, discount, cardContentType)
	let obj2 = {
		format_type: type,
		discount: discount,
		item: { data: [], cate: [] },
		product: { data: [], cate: [] }
	};
	// cardContentType 1：分类 2：商品
	for (const a of arr) {
		console.log('123123', a)
		if (cardContentType == 1) {
			if (a.type == 1) {
				console.log(111)
				obj2.item.cate.push(a.sca_id)
			} else if (a.type == 2) {
				console.log(222)
				obj2.product.cate.push(a.sca_id)
			}
		} else if (cardContentType == 2) {
			if (a.type == 1) {
				obj2.item.data.push({
					id: a.id,
					nums: discount,
					sp_id: a.sp_id
				})
			} else if (a.type == 2) {
				obj2.product.data.push({
					id: a.id,
					nums: discount,
					sp_id: a.sp_id
				})
			}
		}
	}
	console.log('obj2', obj2)
	return obj2;
}

// 处理总次卡
export const handleContent4 = (arr, type, ids, allcount, cardContentType) => {
	console.log('处理打折卡内容----------22222222', arr, ids, allcount, cardContentType)
	let obj4 = {
		format_type: type,
		allcount: allcount,
		item: [],
		product: []
	};
	// cardContentType 1：分类 2：商品
	for (const a of arr) {
		if (a.type == 1) {
			obj4.item.push({
				id: a.id,
				nums: a.nums,
				sp_id: a.sp_id
			})
		} else if (a.type == 2) {
			obj4.product.push({
				id: a.id,
				nums: a.nums,
				sp_id: a.sp_id
			})
		}
	}
	console.log('obj4', obj4)
	return obj4;
}

// 购卡赠送
export const handleContent6 = (arr, type) => {
	console.log('购卡赠送', arr, type)
	let obj6 = {
		item: [
		],
		product: [
		]
	};
	arr.forEach(item => {
		if (item.type == 1) {
			obj6.item.push({
				id: item.id,
				count: item.nums,
				specifications_id: item.sp_id
			})
		} else if (item.type == 2) {
			obj6.product.push({
				id: item.id,
				count: item.nums,
				specifications_id: item.sp_id
			})
		}
	})
	return obj6;
}

//处理时段卡内容
export const handleContent3 = (arr, type, allcount) => {
	console.log('疗程总次卡----------333333', arr, type, allcount)
	let obj1 = {
		format_type: type,
		item: { date: [] },
		product: { date: [] }
	};

	for (const a of arr) {
		if (a.si_id) {
			let arr = obj1["item"]["date"];
			if (!a.specId) {
				arr.push({
					id: a.si_id
				});
			} else {
				if (arr.length <= 0) {
					arr.push({
						id: a.si_id,
						specifications_id: [a.specId]
					});
				} else {
					let findIndex = arr.findIndex((fItem) => {
						return fItem.id === a.si_id;
					});
					if (findIndex === -1) {
						arr.push({
							id: a.si_id,
							specifications_id: [a.specId]
						});
					} else {
						arr[findIndex].specifications_id.push(a.specId);
					}
				}
			}
		} else if (a.sr_id) {
			let arr = obj1["product"]["date"];
			if (!a.specId) {
				arr.push({
					id: a.sr_id
				});
			} else {
				if (arr.length <= 0) {
					arr.push({
						id: a.sr_id,
						specifications_id: [a.specId]
					});
				} else {
					let findIndex = arr.findIndex((fItem) => {
						return fItem.id === a.sr_id;
					});
					if (findIndex === -1) {
						arr.push({
							id: a.sr_id,
							specifications_id: [a.specId]
						});
					} else {
						arr[findIndex].specifications_id.push(a.specId);
					}
				}
			}
		}
	}
	if (type === 4) {
		obj1.allcount = allcount;
	}
	// console.log('obj1-----------------', obj1)
	return obj1;
}

//处理充值添加内容
// [{"fillmoney":"1000","data":{"product":[{"id":342,"count":"2","specifications_id":"规格id"}]}}]
export const rechargeAddContent = (arr) => {
	console.log('处理充值添加内容', arr)
	let resArr = [];
	arr.forEach(a => {
		let data = usedHandleContent(a.list);
		let obj = {
			fillmoney: a.fillmoney,
			data: data
		};
		resArr.push(obj);
	})
	console.log('arrresArr-------------处理充值添加内容结果', resArr)
	return resArr;
}

//处理充值添加内容
/*export const rechargeAddContent = (arr) => {
  console.log('处理充值添加内容',arr)
  let resArr = [];
  for (const a of arr) {
	if (a.specifications) {
	  if (a.specifications.child.length <= 0) continue;
	  let data = usedHandleContent(arr);
	  let obj = {
		fillmoney: a.fillmoney,
		data: data,
	  };
	  resArr.push(obj);
	}
  }
  //console.log('arrresArr-------------处理充值添加内容结果',resArr)
  console.log(resArr)
  return resArr;
};*/

//乘法
export function accMul(arg1, arg2) {
	let m = 0,
		s1 = (arg1 || 0).toString(),
		s2 = (arg2 || 0).toString();
	try {
		m += s1.split(".")[1].length;
	} catch (e) { }
	try {
		m += s2.split(".")[1].length;
	} catch (e) { }
	return (
		(Number(s1.replace(".", "")) * Number(s2.replace(".", ""))) / Math.pow(10, m)
	);
}
//减法
export function accSub(arg1, arg2) {
	let r1, r2, m;
	try {
		r1 = (arg1 || 0).toString().split(".")[1].length;
	} catch (e) {
		r1 = 0;
	}
	try {
		r2 = (arg2 || 0).toString().split(".")[1].length;
	} catch (e) {
		r2 = 0;
	}
	m = Math.pow(10, Math.max(r1, r2));
	return (accMul(arg1, m) - accMul(arg2, m)) / m;
}
export function trunc(num) {
	return num - (num % 1);
}
export function fract(num) {
	let res = num - trunc(num);
	if (res == 0) {
		return 0;
	}
	let decimal = String(num).split(".")[1];
	return decimal;
}
// 是否搜索限制
export function isSearchAstrict(val) {
	if (!val) return false
	let isCN = validate.reg('chinese', val)
	let valid = isCN ? val.length < 2 : val.length < 4
	if (valid) {
		Message.warning('使用关键字搜索时中文最少2位查询,其他至少4位可查询')
	}
	return valid
}
// table刷新拦截
export function onTableRefresh(keys, cb) {
	return async function (params, options) {
		let valid = isArray(keys) ? keys.some(key => isSearchAstrict(params[key])) : isSearchAstrict(params[keys])
		// console.log('onTableRefresh', valid);
		if (valid) {
			options.done()
			return
		}
		if (cb) {
			return cb(params, options)
		}
		const res = await options.next(params)
		if (isObject(res)) {
			options.render(res.list, res.pagination)
			return
		}
		render(res);
	}
}
export default {
	isEmpty,
	formatInput,
	isTrue,
	HandleContent,
	accMul,
	accSub,
	trunc,
	fract,
	isSearchAstrict,
	onTableRefresh,
	getFileType,
	getDecType
}
